﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RenChangYou.DBUtility;
using System.Data.SqlClient;
using RenChangYou.Common;
using System.Configuration;

namespace RenChangYou.DAL
{
    /// <summary>
    /// 数据访问类:DY_User
    /// </summary>
    public partial class DY_User
    {
        public DY_User()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string UserName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DY_User");
            strSql.Append(" where UserName=@UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,256)			};
            parameters[0].Value = UserName;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(RenChangYou.Model.DY_User model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into DY_User(");
                strSql.Append("UserName,UserType,FullName,Mobile,Email,Password,Question,Answer,Sex,Birthday,HomeTown,Country,Province,City,District,Address,CreatedTime,Status,HeadImageName)");
                strSql.Append(" values (");
                strSql.Append("@UserName,@UserType,@FullName,@Mobile,@Email,@Password,@Question,@Answer,@Sex,@Birthday,@HomeTown,@Country,@Province,@City,@District,@Address,@CreatedTime,@Status,@HeadImageName)");
                SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,256),
					new SqlParameter("@UserType", SqlDbType.Char,1),
					new SqlParameter("@FullName", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@Question", SqlDbType.NVarChar,50),
					new SqlParameter("@Answer", SqlDbType.NVarChar,50),
					new SqlParameter("@Sex", SqlDbType.Char,1),
					new SqlParameter("@Birthday", SqlDbType.DateTime),
					new SqlParameter("@HomeTown", SqlDbType.NVarChar,256),
					new SqlParameter("@Country", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.NVarChar,50),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@District", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NChar,10),
					new SqlParameter("@CreatedTime", SqlDbType.DateTime),
					new SqlParameter("@Status", SqlDbType.Char,1),
                    new SqlParameter("@HeadImageName", SqlDbType.NVarChar,50)};
                parameters[0].Value = model.UserName;
                parameters[1].Value = model.UserType;
                parameters[2].Value = model.FullName;
                parameters[3].Value = model.Mobile;
                parameters[4].Value = model.Email;
                //parameters[5].Value = model.Password;
                //parameters[5].Value = Common.Common.MD5Encrypt(string.Format("{0}{1}", model.Password, ConfigurationManager.AppSettings["CONST_MD5_KEY"].ToString()));
                parameters[5].Value = Common.Common.MD5Encrypt(string.Format("{0}{1}", model.Password, "zhangzhao188@126.com"));
                parameters[6].Value = model.Question;
                parameters[7].Value = model.Answer;
                parameters[8].Value = model.Sex;
                parameters[9].Value = model.Birthday;
                parameters[10].Value = model.HomeTown;
                parameters[11].Value = model.Country;
                parameters[12].Value = model.Province;
                parameters[13].Value = model.City;
                parameters[14].Value = model.District;
                parameters[15].Value = model.Address;
                //parameters[16].Value = model.CreatedTime;
                parameters[16].Value = DateTime.Now;
                //parameters[17].Value = model.Status;
                parameters[17].Value = 0;
                parameters[18].Value = model.HeadImageName;

                int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RenChangYou.Model.DY_User model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DY_User set ");
            strSql.Append("UserType=ISNULL(@UserType, UserType),");
            strSql.Append("FullName=ISNULL(@FullName, FullName),");
            strSql.Append("Mobile=ISNULL(@Mobile, Mobile),");
            strSql.Append("Email=ISNULL(@Email, Email),");
            strSql.Append("Password=ISNULL(@Password, Password),");
            strSql.Append("Question=ISNULL(@Question, Question),");
            strSql.Append("Answer=ISNULL(@Answer, Answer),");
            strSql.Append("Sex=ISNULL(@Sex, Sex),");
            strSql.Append("Birthday=ISNULL(@Birthday, Birthday),");
            strSql.Append("HomeTown=ISNULL(@HomeTown, Hometown),");
            strSql.Append("Country=@Country,");
            strSql.Append("Province=@Province,");
            strSql.Append("City=@City,");
            strSql.Append("District=@District,");
            strSql.Append("Address=@Address,");
            strSql.Append("CreatedTime=@CreatedTime,");
            strSql.Append("Status=@Status,");
            strSql.Append("HeadImageName=@HeadImageName");
            strSql.Append(" where UserName=@UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserType", SqlDbType.Char,1),
					new SqlParameter("@FullName", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@Question", SqlDbType.NVarChar,50),
					new SqlParameter("@Answer", SqlDbType.NVarChar,50),
					new SqlParameter("@Sex", SqlDbType.Char,1),
					new SqlParameter("@Birthday", SqlDbType.DateTime),
					new SqlParameter("@HomeTown", SqlDbType.NVarChar,256),
					new SqlParameter("@Country", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.NVarChar,50),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@District", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NChar,10),
					new SqlParameter("@CreatedTime", SqlDbType.DateTime),
					new SqlParameter("@Status", SqlDbType.Char,1),
					new SqlParameter("@UserName", SqlDbType.NVarChar,256),
                    new SqlParameter("@HeadImageName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.UserType;
            parameters[1].Value = model.FullName;
            parameters[2].Value = model.Mobile;
            parameters[3].Value = model.Email;
            parameters[4].Value = model.Password;
            parameters[5].Value = model.Question;
            parameters[6].Value = model.Answer;
            parameters[7].Value = model.Sex;
            parameters[8].Value = model.Birthday;
            parameters[9].Value = model.HomeTown;
            parameters[10].Value = model.Country;
            parameters[11].Value = model.Province;
            parameters[12].Value = model.City;
            parameters[13].Value = model.District;
            parameters[14].Value = model.Address;
            parameters[15].Value = model.CreatedTime;
            parameters[16].Value = model.Status;
            parameters[17].Value = model.UserName;
            parameters[18].Value = model.HeadImageName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string UserName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DY_User ");
            strSql.Append(" where UserName=@UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,256)			};
            parameters[0].Value = UserName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string UserNamelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DY_User ");
            strSql.Append(" where UserName in (" + UserNamelist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RenChangYou.Model.DY_User GetModel(string UserName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserName,UserType,FullName,Mobile,Email,Password,Question,Answer,Sex,Birthday,HomeTown,Country,Province,City,District,Address,CreatedTime,Status,HeadImageName from DY_User ");
            strSql.Append(" where UserName=@UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,256)			};
            parameters[0].Value = UserName;

            RenChangYou.Model.DY_User model = new RenChangYou.Model.DY_User();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RenChangYou.Model.DY_User DataRowToModel(DataRow row)
        {
            RenChangYou.Model.DY_User model = new RenChangYou.Model.DY_User();
            if (row != null)
            {
                if (row["UserName"] != null)
                {
                    model.UserName = row["UserName"].ToString();
                }
                if (row["UserType"] != null)
                {
                    model.UserType = Convert.ToInt32(row["UserType"].ToString());
                }
                if (row["FullName"] != null)
                {
                    model.FullName = row["FullName"].ToString();
                }
                if (row["Mobile"] != null)
                {
                    model.Mobile = row["Mobile"].ToString();
                }
                if (row["Email"] != null)
                {
                    model.Email = row["Email"].ToString();
                }
                if (row["Password"] != null)
                {
                    model.Password = row["Password"].ToString();
                }
                if (row["Question"] != null)
                {
                    model.Question = row["Question"].ToString();
                }
                if (row["Answer"] != null)
                {
                    model.Answer = row["Answer"].ToString();
                }
                if (row["Sex"] != null)
                {
                    model.Sex = row["Sex"].ToString();
                }
                if (row["Birthday"] != null && row["Birthday"].ToString() != "")
                {
                    model.Birthday = DateTime.Parse(row["Birthday"].ToString());
                }
                if (row["HomeTown"] != null)
                {
                    model.HomeTown = row["HomeTown"].ToString();
                }
                if (row["Country"] != null)
                {
                    model.Country = row["Country"].ToString();
                }
                if (row["Province"] != null)
                {
                    model.Province = row["Province"].ToString();
                }
                if (row["City"] != null)
                {
                    model.City = row["City"].ToString();
                }
                if (row["District"] != null)
                {
                    model.District = row["District"].ToString();
                }
                if (row["Address"] != null)
                {
                    model.Address = row["Address"].ToString();
                }
                if (row["CreatedTime"] != null && row["CreatedTime"].ToString() != "")
                {
                    model.CreatedTime = DateTime.Parse(row["CreatedTime"].ToString());
                }
                if (row["Status"] != null)
                {
                    model.Status = row["Status"].ToString();
                }
                if (row["HeadImageName"] != null)
                {
                    model.HeadImageName = row["HeadImageName"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserName,UserType,FullName,Mobile,Email,Password,Question,Answer,Sex,Birthday,HomeTown,Country,Province,City,District,Address,CreatedTime,Status,HeadImageName ");
            strSql.Append(" FROM DY_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" UserName,UserType,FullName,Mobile,Email,Password,Question,Answer,Sex,Birthday,HomeTown,Country,Province,City,District,Address,CreatedTime,Status,HeadImageName ");
            strSql.Append(" FROM DY_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DY_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.UserName desc");
            }
            strSql.Append(")AS Row, T.*  from DY_User T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "DY_User";
            parameters[1].Value = "UserName";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod



        #region
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int RegisterUser(RenChangYou.Model.DY_User user)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@userName", SqlDbType.NVarChar, 256),
                    new SqlParameter("@password", SqlDbType.VarChar, 50),
                    new SqlParameter("@birthday", SqlDbType.DateTime),
                    new SqlParameter("@sex", SqlDbType.Char),
                    };
            parameters[0].Value = user.UserName;
            //parameters[1].Value = Common.Common.MD5Encrypt(string.Format("{0}{1}", user.Password, ConfigurationManager.AppSettings["CONST_MD5_KEY"].ToString()));
            parameters[1].Value = Common.Common.MD5Encrypt(string.Format("{0}{1}", user.Password, "zhangzhao188@126.com"));
            parameters[2].Value = user.Birthday;
            parameters[3].Value = user.Sex;
            int rowsaffected; //受影响行数
            return DbHelperSQL.RunProcedure("RegisterUser", parameters, out rowsaffected);
        }

        ///// <summary>
        ///// 检查用户名是否存在
        ///// </summary>
        ///// <param name="userName"></param>
        ///// <returns>1存在，0 不存在</returns>
        //public int CheckTheAccountIsCanUse(string userName)
        //{
        ////    SqlParameter[] parameters={
        ////                                new SqlParameter("@username",SqlDbType.VarChar,50)
        ////                              };
        ////    parameters[0].Value=userName;
        ////    int rowsaffected;
        ////    return DbHelperSQL.RunProcedure("up_Web_checkTheAccountIsCanUse", parameters, out rowsaffected);
        //    string strSql = "select * from dbo.DY_User where userName=@username";
        //    SqlParameter[] parameters = { 
        //        new SqlParameter("@username",SqlDbType.VarChar,50)
        //    };
        //    DataSet ds = DbHelperSQL.Query(strSql);
        //    if (ds != null && ds.Tables != null && ds.Tables[0].Rows.Count > 0)
        //    {
        //        return 1;
        //    }
        //    else
        //    {
        //        return 0;
        //    }
        //}

        public DataTable UserLogin(string userName, string password)
        {
            string strSql = "SELECT UserType,UserName FROM dbo.DY_User WHERE UserName=@UserName AND Password=@password";
            SqlParameter[] parameters = { 
                new SqlParameter("@UserName",SqlDbType.VarChar,50),
                new SqlParameter("@password",SqlDbType.VarChar,32)
            };

            parameters[0].Value = userName;
            //parameters[1].Value = Common.Common.MD5Encrypt(string.Format("{0}{1}", password, ConfigurationManager.AppSettings["CONST_MD5_KEY"].ToString()));
            parameters[1].Value = Common.Common.MD5Encrypt(string.Format("{0}{1}", password, "zhangzhao188@126.com"));
            return DbHelperSQL.Query(strSql, parameters).Tables[0];

        }

        public int UpdPwd(string account, string oldPwd, string newPwd)
        {
            string strSql = "UPDATE DY_User SET Password=@newPwd WHERE UserName=@UserName AND Password=@oldPwd";
            SqlParameter[] parameters = { 
                new SqlParameter("@UserName",SqlDbType.VarChar,50),       
                new SqlParameter("@oldPwd",SqlDbType.VarChar,32),
                new SqlParameter("@newPwd",SqlDbType.VarChar,32)
            };
            parameters[0].Value = account;
            parameters[1].Value = oldPwd;
            parameters[2].Value = newPwd;
            return DbHelperSQL.ExecuteSql(strSql, parameters);

        }


        #endregion

    }
}
